package cn.appinfodb.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import cn.appinfodb.pojo.BackendUser;
import cn.appinfodb.service.BackendUserService;
import cn.appinfodb.tools.Constants;

@Controller
@RequestMapping(value="/manager")
public class BackendLoginController {
	private Logger logger = Logger.getLogger(BackendLoginController.class);
	
	@Resource
	private BackendUserService backendUserService;
	
	@RequestMapping(value="/login")
	public String login(){
		logger.debug("LoginController welcome APPInfo==================");
		return "backendlogin";
	}
	@RequestMapping(value="/dologin",method=RequestMethod.POST)
	public String doLogin(@RequestParam String userCode,@RequestParam String userPassword,HttpServletRequest request,HttpSession session) throws Exception{
		logger.debug("doLogin====================================");
		//����service�����������û�ƥ��
		BackendUser backenduser = backendUserService.login(userCode, userPassword);
		if(null != backenduser){//��¼�ɹ�
			//ҳ����ת��frame.jsp��
			session.setAttribute(Constants.USER_SESSION, backenduser);
			return "redirect:/manager/flatform/main";
		}else{
			//ҳ����ת��login.jsp��������ʾ��Ϣ--ת��
			request.setAttribute("error", "�û��������벻��ȷ");
			return "login";
		}
	}
	@RequestMapping(value="/flatform/main")
	public String main(){
		return "backend/main";
	}

	
	@RequestMapping(value="/logout")
	public String logout(HttpSession session){
		//���session
		session.removeAttribute(Constants.USER_SESSION);
		return "backendlogin";
	}
	

}
